package sg.edu.nus.iss.cats.dataaccess.hibernate;

import java.util.List;

import sg.edu.nus.iss.cats.dataaccess.DepartmentDAO;
import sg.edu.nus.iss.cats.exception.HibernateAccessLayerException;
import sg.edu.nus.iss.cats.model.Department;

public class DepartmentDAOImpl extends AbstractDao implements DepartmentDAO {

	public DepartmentDAOImpl() {

		super();
	}

	public Department findDepartment(String departmentId)
			throws HibernateAccessLayerException {
		System.out.println("Finding user" + departmentId);
		try {
			return ((Department) super.find(Department.class, departmentId));

		} catch (Throwable t) {
			throw new HibernateAccessLayerException(t.toString());

		}

	}

	public Department[] findAllDepartments()
			throws HibernateAccessLayerException {
		
		try {
			List list = super.findAll(Department.class);
			Department[] depts = new Department[list.size()];
			list.toArray(depts);
			return depts;

		} catch (Throwable t) {

			throw new HibernateAccessLayerException(t.toString());

		}
	}

	public void insertDepartment(Department department)
			throws HibernateAccessLayerException {
		System.out.println("Inserting department"
				+ department.getDepartmentId());
		try {
			super.saveOrUpdate(department);
		} catch (Throwable t) {
			throw new HibernateAccessLayerException(t.toString());

		}
	}

	public void updateDepartment(Department department)
			throws HibernateAccessLayerException {

		System.out
				.println("Updating department" + department.getDepartmentId());
		try {
			super.saveOrUpdate(department);
		} catch (Throwable t) {
			throw new HibernateAccessLayerException(t.toString());

		}

	}

	public void deleteDepartment(String departmentId)
			throws HibernateAccessLayerException {

		System.out.println("Deleting department" + departmentId);
		Department department;
		try {
			department = findDepartment(departmentId);
			super.saveOrUpdate(department);
		} catch (Throwable t) {
			throw new HibernateAccessLayerException(t.toString());

		}
	}

}
